<?
  $this->load->helper('file');


  $s=$_GET['s'];
  
  if($s == "Поиск"){
  $s="";
  }
  
  $s=trim($s);
  if ($s=='' || strlen($s) < 2){
  $false="ok";


}
?>
  <script src="/scripts/jquery-latest.js"></script>
  <link rel="stylesheet" href="/css/tabs.css" type="text/css" media="screen" >
  <script type="text/javascript" src="/scripts/ui.core.js"></script>
  <script type="text/javascript" src="/scripts/ui.tabs.js"></script>


  <script>
  $(document).ready(function(){
    $("#example > ul").tabs();
  });
  </script>
  
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="100%" valign="top" style="padding: 0 10px 0 10px">
  
  <h1>Результаты поиска</h1>
 

      <p>
          <form method="get" action="">
          <input name="c" value="store" type="hidden">
          <input name="m" value="search" type="hidden">
          <input name="s" type="text" value="<?=$s?>" style="position: relative; width: 40%;margin-top:1em">
          <input value="Найти" type="submit">
          </form>
      </p>

 
 
<? 
if($false != "ok"){   

$all=array();
      $query = $this->db->query("select id,father,name from store where level=4 and available=1 and(name  like '%".$s."%' or title  like '%".$s."%' or subhead like '%".$s."%' or brand like '%".$s."%' or body like '%".$s."%') ORDER BY id");
                   if($query->num_rows > 0){  
                      foreach ($query->result() as $add){
                      array_push($all, $add->id);
                      }
                    } 
                                  
        $query = $this->db->query("select father from articles where article like '%".$s."%' "); 
                  if($query->num_rows > 0){
                      foreach ($query->result() as $add){
                      
                                          
                        $available = $this->db->query("select available from store where id='$add->father'");
                        $available = $available->row();
                        
                        
                        
                              if($available->available == 1){
                              
                              array_push($all, $add->father);
                            }
                       }
                  }
                  
                  
                  
                                     

      $all=array_unique($all);
      
     if ( count($all)== 0){
     $false="ok";
     }


      foreach ($all as $id){
      $father = $this->db->query("SELECT id,father FROM store WHERE level=4 AND id='$id'");
      $father = $father->row();
      $level3=$father->father;
      
      $level2 = $this->db->query("SELECT id,father FROM store WHERE level=3 AND id='$level3'");
      $level2 = $level2->row();
      $level2 = $level2->father; 
      
      $level1 = $this->db->query("SELECT id,father FROM store WHERE level=2 AND id='$level2'");
      $level1 = $level1->row();
      $level1 = $level1->father; 
      
       $levels["$id"]=array("level1" => "$level1", "level2" => "$level2", "level3" => "$level3");
      
      }
      
      $levels1 = array();
      
      foreach ($levels as $l){
          array_push($levels1, $l[level1]);
      }
      
      $levels1=array_unique($levels1);
}

if($false != "ok"){                          
?>
  
 <p>   
        <div id="example" class="flora">
            <ul>
                
                <? $i=0; ?>
                <? foreach ($levels1 as $l1){
                $i=$i+1; 
                          $name = $this->db->query("SELECT name FROM store WHERE level=1 AND id='$l1'");
                          $name = $name->row();
                          
                                $ii=0;
                                foreach ($levels as $key => $value) {
                                if($value[level1] == $l1){
                                $ii=$ii+1;
                                }
                              }

                ?>
                <li><a href="#fragment-<?=$i?>"><div class="unactive"><?=$name->name?> (<?=$ii?>)</div></a></li>
                <?}?>
            </ul>
            
            <br>
            
            
            
                <? $i=0; ?>
                <? foreach ($levels1 as $levels1){
                $i=$i+1; 
                  ?><div id="fragment-<?=$i?>"><?
   
                                foreach ($levels as $key => $value) {
                                if($value[level1] == $levels1){
                                
                                $level1 = $this->db->query("SELECT link FROM store WHERE  id='$value[level1]'");
                                $level1 = $level1->row();
                                
                                $level2 = $this->db->query("SELECT link FROM store WHERE  id='$value[level2]'");
                                $level2 = $level2->row();
                                
                                $level3 = $this->db->query("SELECT link FROM store WHERE  id='$value[level3]'");
                                $level3 = $level3->row();
                                
                                    $name = $this->db->query("SELECT name,link,id,brand,subhead FROM store WHERE level=4 AND id='$key'");
                                    $row = $name->row();
                                    
                                //Определяем цену/цены
                               $price=$this->Store_model->get_price($row->id);
                              
                                        
                                    
                                
                                         if($levels1 == 1){?>
                                         
                                         
                                          <div>
                                            <a href="/store/catalog/<?=$level1->link?>/<?=$level2->link?>/<?=$level3->link?>/<?=$row->id?>/">
                                            <img src="/images/store/preview/<?=$row->id?>_1.jpg" border="0">
                                            </a>
                                          </div>
                                          <div><small><a href="/store/catalog/<?=$level1->link?>/<?=$level2->link?>/<?=$level3->link?>/<?=$row->id?>/"><?=$row->name?></a></small></div>
                                          
                                          <? if($row->subhead !=''){?>
                                            <div><small><?=$row->subhead?></small></div>
                                          <?}?>
                                          
                                          <div><small><?if($price !=0){echo "<b>$price грн</b>";}else{echo "<font color=\"red\">Поставка ожидается</font>";} ?></small></div>
                                          <br>
                                         
                                         <?}else{?>
                                         
                                         <div class="side">
                                          <div>
                                            <? if (read_file("./images/store/preview/$row->id" . '_1.jpg')){ ?>
                                             <a href="/store/catalog/<?=$level1->link?>/<?=$level2->link?>/<?=$level3->link?>/<?=$row->id?>/"><img src="/images/store/preview/<?=$row->id?>_1.jpg" border="0"></a>
                                            <? } ?>
                                          </div>
                                      
                                            <div><small><?=$row->brand?> <a href="/store/catalog/<?=$level1->link?>/<?=$level2->link?>/<?=$level3->link?>/<?=$row->id?>/"><?=$row->name?></a></small></div>
                                            
                                              <? if($row->subhead !=''){?>
                                                <div><small><?=$row->subhead?></small></div>
                                              <?}?>
                                             
                                            
                                            <div><small><?if($price !=0){echo "<b>$price грн</b>";}else{echo "<font color=\"red\">Поставка ожидается</font>";} ?></small></div>
                                        </div>
                                        <?}
                                }
                              }
                   ?></div><?       
                }
                ?>

        </div>
</p>


<?}else{?>

<p>По вашему запросу ничего не найдено.</p>

<?}?>
   

   </tr>
   </table>